package org.ohos.rajawali3d.examples.examples.loaders;

import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import opengl.platform.ohos.Log;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.loader.LoaderGCode;
import org.rajawali3d.materials.Material;
import org.rajawali3d.primitives.Line3D;
import org.rajawali3d.util.RajLog;

public class LoaderGCodeFragment extends AExampleFragment {

    @Override
    public AExampleRenderer createRenderer() {
        return new GCodeRenderer(getContext(), this);
    }

    private final class GCodeRenderer extends AExampleRenderer {

        GCodeRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            setFrameRate(60);
            ResourceManager resManager = mContext.getResourceManager();
            RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/"
                    + "calibrationcube_404020_psm_pla35.gcode");
            LoaderGCode gCodeParser = new LoaderGCode(getResourceManager(), getTextureManager(), rawFileEntry);
            try {
                Object3D gCode3D = gCodeParser.parse(mContext).getParsedObject();
                if (null != gCode3D) {
                    Line3D firstPos = (Line3D) gCode3D.getChildAt(0);
                    Line3D lastPos;
                    if (gCode3D.getNumChildren() < 2) {
                        lastPos = firstPos;
                    } else {
                        lastPos = (Line3D) gCode3D.getChildAt(gCode3D
                                .getNumChildren() - 1);
                    }
                    getCurrentCamera().setPosition(0, 0,
                            lastPos.getPoint(0).z + 150);
                    getCurrentCamera().setLookAt(0, 0, 0);
                    float scaleFactor = 0.7f;
                    gCode3D.setScale(scaleFactor);
                    gCode3D.setPosition(-firstPos.getPoint(0).x
                            * scaleFactor, -firstPos.getPoint(0).y
                            * scaleFactor, 0);
                    gCode3D.setRotation(360 - 35, 0, 360 - 15);
                    Material mat = new Material();
                    for (int i = 0; i < gCode3D.getNumChildren(); i++) {
                        gCode3D.getChildAt(i).setMaterial(mat);
                    }
                    getCurrentScene().addChild(gCode3D);
                }
            } catch (Exception e) {
                RajLog.e("error init'ing gcode GL scene:\n" + Log.getStackTraceString(e));
            }
        }
    }

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        DirectionalLayout ll = new DirectionalLayout(getContext());
        ComponentContainer.LayoutConfig lc = ll.getLayoutConfig();
        lc.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        lc.height = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        ll.setOrientation(DirectionalLayout.VERTICAL);
        ll.setAlignment(LayoutAlignment.BOTTOM);

        Text label = new Text(getContext());
        ComponentContainer.LayoutConfig layoutConfig = label.getLayoutConfig();
        layoutConfig.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        layoutConfig.height = DirectionalLayout.LayoutConfig.MATCH_CONTENT;
        label.setText(ResourceTable.String_gcode_fragment_by);
        label.setTextSize(42);
        label.setMultipleLine(true);
        label.setTextAlignment(TextAlignment.CENTER);
        label.setTextColor(new Color(0xFFFFFFFF));
        label.setHeight(100);
        ll.addComponent(label);
        mLayout.addComponent(ll);
        return mLayout;
    }

}
